import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.JSeparator;
import java.awt.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;


public class VueRechercheParMotsCles extends Vue {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JButton buttonFermer;
	private JSeparator separator;
	private List listMotsCles;
	private List listOuvrages;
	private List listArticles;
	private TreeMap<String, MotsCles> motsCles;

	@SuppressWarnings("deprecation")
	public VueRechercheParMotsCles(Controleur controleur) {
		super(controleur);

		setTitle("Recherche par mots-clés");

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
		setBounds(100, 100, 450, 636);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		buttonFermer = new JButton("Fermer");
		buttonFermer.setEnabled(false);
		buttonFermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			//().fermerVue(VueSaisiePeriodique.this);
				}
		});
		buttonFermer.setBounds(144, 562, 107, 25);
		contentPane.add(buttonFermer);
		

		JLabel lblSlctionnezLesMots = new JLabel("Sélectionnez le(s) mot(s) pour la recherche");

		lblSlctionnezLesMots.setBounds(59, 23, 312, 14);
		contentPane.add(lblSlctionnezLesMots);
		
		listMotsCles = new List();
		listMotsCles.setMultipleSelections(true);
		listMotsCles.setBounds(58, 49, 287, 101);
		contentPane.add(listMotsCles);
		
		
		separator = new JSeparator();
		separator.setBounds(0, 233, 434, 2);
		contentPane.add(separator);
		
		JButton btnRechercher = new JButton("Rechercher");
		btnRechercher.setBounds(71, 186, 107, 23);
		contentPane.add(btnRechercher);
		btnRechercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String[] lesMotsCles = listMotsCles.getSelectedItems();
				motsCles=new TreeMap<String, MotsCles>();
				int i=0;
				while (i<lesMotsCles.length){
					//System.out.println(lesMotsCles.length);
					MotsCles m = new MotsCles(lesMotsCles[i]);
					motsCles.put(lesMotsCles[i], m);
					i=i+1;
				}
				chercheMotsClesOuvrage(motsCles);
				chercheMotsClesArticles(motsCles);
				
				
			}
		});
		
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.setBounds(251, 186, 94, 23);
		contentPane.add(btnAnnuler);
		
		listOuvrages = new List();
		listOuvrages.setEnabled(false);
		listOuvrages.setMultipleSelections(false);
		listOuvrages.setBounds(144, 252, 201, 101);
		contentPane.add(listOuvrages);
		
		JLabel lblOuvrages = new JLabel("Ouvrage(s)");
		lblOuvrages.setEnabled(false);
		lblOuvrages.setHorizontalAlignment(SwingConstants.RIGHT);
		lblOuvrages.setBounds(32, 291, 82, 14);
		contentPane.add(lblOuvrages);
		
		JLabel lblArticles = new JLabel("Article(s)");
		lblArticles.setEnabled(false);
		lblArticles.setBounds(59, 451, 55, 14);
		contentPane.add(lblArticles);
		
		listArticles = new List();
		listArticles.setBounds(144, 409, 201, 115);
		contentPane.add(listArticles);
		

		
	}

	@SuppressWarnings("deprecation")
	public void alimenteListeMot(TreeMap<String, MotsCles> motsCles) {
		
		Set<String> cles = motsCles.keySet();
		Iterator<String> it = cles.iterator();
		listMotsCles.clear();
		while (it.hasNext()){
		   String cle = (String) it.next(); // tu peux typer plus finement ici
		   MotsCles valeur = motsCles.get(cle); // tu peux typer plus finement ici
		   String motcle = valeur.getMot();
		   listMotsCles.add(motcle);
		}
		
	}
	
	public void chercheMotsClesOuvrage(TreeMap<String, MotsCles> motsCles)  {
		boolean pareil = false;
		
		HashMap<String, Ouvrage> ouvrages; 
		ouvrages = this.getControleur().getOuvrages();

		
		Set<String> clesOuvrages = ouvrages.keySet();
		Iterator<String> itOuvrages = clesOuvrages.iterator();
		while (itOuvrages.hasNext())  {
			String cleOuvrage = (String) itOuvrages.next();
			Ouvrage ouvrage = ouvrages.get(cleOuvrage);
			TreeMap <String, MotsCles> motsClesOuv = ouvrage.getMotsCles();
			
			Set<String> clesMotsCles = motsCles.keySet();
			Iterator<String> itMotsCles = clesMotsCles.iterator();
			while (itMotsCles.hasNext() && !pareil)  {
				String cleMotsCles = (String) itMotsCles.next();
				//MotsCles mots = motsCles.get(cleMotsCles);
				
				Set<String> clesMotsClesOuv = motsClesOuv.keySet();
				Iterator<String> itMotsClesOuv = clesMotsClesOuv.iterator();
				while (itMotsClesOuv.hasNext() && !pareil)  {
					String cleMotsClesOuv = (String) itMotsClesOuv.next();
					if (cleMotsCles == cleMotsClesOuv)  {
						listOuvrages.add(ouvrage.getTitre());
						pareil = true;
					}
				}
			}
			pareil = false;
		}
	}
	
	public void chercheMotsClesArticles(TreeMap<String, MotsCles> motsCles)  {
	/*	TreeMap<String, Article> articles; 
		articles = this.getControleur().getArticles();
		
		Set<String> cles = articles.keySet();
		Iterator<String> it = cles.iterator();
		while (it.hasNext())  {
			String cle = (String) it.next();
			Article article = articles.get(cle);
			listOuvrages.add(article.getTitre());
		}
	}	*/	
	
	boolean pareil = false;
	
	TreeMap<String, Article> articles; 
	articles = this.getControleur().getArticles();

	
	Set<String> clesArticles = articles.keySet();
	Iterator<String> itArticles = clesArticles.iterator();
	while (itArticles.hasNext())  {
		String cleArticle = (String) itArticles.next();
		Article article = articles.get(cleArticle);
		TreeMap <String, MotsCles> motsClesArt = article.getMotsCless();
		
		Set<String> clesMotsCles = motsCles.keySet();
		Iterator<String> itMotsCles = clesMotsCles.iterator();
		while (itMotsCles.hasNext() && !pareil)  {
			String cleMotsCles = (String) itMotsCles.next();
			//MotsCles mots = motsCles.get(cleMotsCles);
			
			Set<String> clesMotsClesArt = motsClesArt.keySet();
			Iterator<String> itMotsClesArt = clesMotsClesArt.iterator();
			while (itMotsClesArt.hasNext() && !pareil)  {
				String cleMotsClesArt = (String) itMotsClesArt.next();
				if (cleMotsCles == cleMotsClesArt)  {
					listArticles.add(article.getTitre());
					pareil = true;
				}
			}
		}
		pareil = false;
	}
}
}
